/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "rep_logs_hyperlinks.h"
#include "rep_logs_pages.h"
#include "rep_general.h"
#include "GR_write_html.h"


void
write_body_hmvis_html_file(GraphFrame *gf,FILE *fp,int nhlinks,WebStatPtr msn);
void
write_body_shmvis_html_file(GraphFrame *gf,FILE *fp,int nhlinks,
			    WebStatPtr msn);
void
write_body_independent_hmvis_html_file(GraphFrame *gf,FILE *fp,int nhlinks,
				  WebStatPtr msn);

int 
report_hyperlinks_most_visited(GraphFrame *gf, char *name_file,int nhlinks,
			       WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Most Visited Hyperlinks");
  write_start_body_html_file(fp,"#ffffff");
  write_body_hmvis_html_file(gf,fp,nhlinks,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_hmvis_html_file(GraphFrame *gf,FILE *fp,int nhlinks,WebStatPtr msn)
{
  LNode_type *nodes = get_list_vertices(gf);
  LNode_type *ptr = nodes->back;
  time_t now;
  PEDGE *varray = NULL;
  int tlinks = 0;
  int tnlinks = 0;
  int i = 0;
  char ntitle[100];
  long thits = LOGR_get_total_site_naccesses(msn);

  for(; ptr != nodes; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      LNode_type *peo, *ledges_out = get_edges_out(v);
      for(peo = ledges_out->back ; peo != ledges_out ; peo = peo->back)
	{
	  PEDGE e = (PEDGE)peo->info;
	  char *we = get_weight_edge(e);
	  int cedge = we ? atoi(we) : 0;

	  if(get_edge_attribute(e) == 'z')
	    continue;

	  if(cedge > 0)
	      tlinks++;
	}
    }

  varray = tlinks ? (PEDGE *)calloc(tlinks,sizeof(PEDGE)) : NULL;

  if(!varray && tlinks > 0) {
      fprintf(stderr,"Out of Memory write_body_hmvis_html_file\n");
      abort();
  }

  now = time(NULL);
  write_header_html_file(fp,1,"List of Most Visited Hyperlinks");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Total Number of Requests : ");
  fprintf(fp, " %ld \n",thits);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Hyperlinks : ");
  fprintf(fp, " %d \n",tlinks);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RMVHString);
  write_horizontal_rule_html_file(fp);

  if(tlinks <= 0) 
      return;

  i = 0;

  for(ptr = nodes->back; ptr != nodes; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      LNode_type *peo, *ledges_out = get_edges_out(v);
      for(peo = ledges_out->back ; peo != ledges_out ; peo = peo->back)
	{
	  PEDGE e = (PEDGE)peo->info;
	  char *we = get_weight_edge(e);
	  int cedge = we ? atoi(we) : 0;

	  if(get_edge_attribute(e) == 'z')
	    continue;

	  if(cedge > 0)
	      varray[i++] = e;
	}
    } 

  if(nhlinks > tlinks)
      tnlinks = tlinks;
  else
      tnlinks = nhlinks;

  sprintf(ntitle,"%d Most Visited Hyperlinks", tnlinks);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string3(fp,"Num","Hyperlink","Hits","Percentage");

  for(i = 1; i<=tnlinks ; i++)
  {
    PEDGE e = (PEDGE)randomized_select((GPTR *)varray,i-1,tlinks-1,1,
				       is_weight_lower,is_weight_bigger);
    
    if(e)
    {
      PNODE u = get_from_vertex(e);
      PNODE v = get_to_vertex(e);
      char * sanchor = get_label_edge(e);
      const char *utit = get_attribute(u,"title");
      const char *vtit = get_attribute(v,"title");
      char *ula = get_label_vertex(u);
      char *vla = get_label_vertex(v);
      char *scount = get_weight_edge(e);
      int cedge = scount ? atoi(scount) : 0;  
      float ave_nacc = ((float)cedge/(float)thits)*100.0;
      char snum[20],save[20];
      sprintf(snum,"%d", i);
      if(!vtit)
	  vtit = "No Info";
      if(!utit)
	  utit = "No Info";
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      write_row_num_hyperlink_string2(fp,snum,ula,utit,vla,vtit,sanchor,
				      scount,save);
    }
  }
  write_end_table_html_file(fp); 
  free(varray); varray = NULL; 
}

int 
report_independent_hyperlinks_most_visited(GraphFrame *gf, char *name_file,
				      int nhlinks,WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Most Visited Independent Hyperlinks");
  write_start_body_html_file(fp,"#ffffff");
  write_body_independent_hmvis_html_file(gf,fp,nhlinks,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_independent_hmvis_html_file(GraphFrame *gf,FILE *fp,int nhlinks,
				  WebStatPtr msn)
{
  LNode_type *independent_links = LOGR_get_list_independent_links(msn);
  LNode_type *ptr = NULL;
  time_t now;
  WebIndependentLinkPtr *varray = NULL;
  WebIndependentLinkPtr *varray2 = NULL;
  int tlinks = independent_links ? count_llist(independent_links) : 0;
  int thlinks = 0;
  int tnhlinks = 0;
  int tnlinks = 0;
  int i = 0,j=0;
  char ntitle[100];
  long thits = LOGR_get_total_site_naccesses(msn);

  for(ptr = independent_links->back; ptr != independent_links; ptr=ptr->back)
  {
    WebIndependentLinkPtr il = (WebIndependentLinkPtr)ptr->info;
    char *mime = (char *)LOGR_get_link_target_mime(il);
    if(!mime)
	continue;

    if(!strcmp(mime,"text/html") || !strcmp(mime,"www/unknown") )
	thlinks++;
    else
	tnhlinks++;
  } 

  varray = thlinks ? (WebIndependentLinkPtr*)calloc(thlinks,
					   sizeof(WebIndependentLinkPtr)) : NULL;

  if(!varray && thlinks > 0) {
      fprintf(stderr,"Out of Memory write_body_hmvis_html_file\n");
      abort();
  }

  varray2 = tnhlinks ? (WebIndependentLinkPtr*)calloc(tnhlinks,
					   sizeof(WebIndependentLinkPtr)) : NULL;

  if(!varray2 && tnhlinks > 0) {
      fprintf(stderr,"Out of Memory write_body_hmvis_html_file\n");
      abort();
  }


  for(ptr = independent_links->back; ptr != independent_links; ptr=ptr->back)
  {
    WebIndependentLinkPtr il = (WebIndependentLinkPtr)ptr->info;
    char *mime = (char *)LOGR_get_link_target_mime(il);
    if(!mime)
	continue;

    if(!strcmp(mime,"text/html") || !strcmp(mime,"www/unknown") )
	varray[i++]=il;
    else
	varray2[j++]=il;
  } 

  now = time(NULL);
  write_header_html_file(fp,1,"List of Most Visited Independent Hyperlinks");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Total Number of Requests : ");
  fprintf(fp, " %ld \n",thits);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Independent Hyperlinks : ");
  fprintf(fp, " %d \n",tlinks);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RMVIHString);
  write_horizontal_rule_html_file(fp);

  if(thlinks <= 0) 
      return;

  i = 0;

  if(nhlinks > thlinks)
      tnlinks = thlinks;
  else
      tnlinks = nhlinks;

  sprintf(ntitle,"%d Most Visited Independent Hyperlinks", tnlinks);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string3(fp,"Num","Hyperlink","Hits","Percentage");

  for(i = 1; i<=tnlinks ; i++)
  {
    WebIndependentLinkPtr il = (WebIndependentLinkPtr)randomized_select((GPTR *)varray,
				       i-1,thlinks-1,1,
				       LOGR_is_access_il_lower,
				       LOGR_is_access_il_bigger);
    
    if(il)
    {
      const char *utit = LOGR_get_link_source_title(il);
      const char *vtit = LOGR_get_link_target_title(il);
      char *ula = (char *)LOGR_get_link_source_url(il);
      char *vla = (char *)LOGR_get_link_target_url(il);
      long nacc = LOGR_get_link_naccesses(il);
      float ave_nacc = ((float)nacc/(float)thits)*100.0;
      char snum[20],scount[20],save[20];      
      sprintf(snum,"%d", i);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      write_row_num_hyperlink_string2(fp,snum, ula,utit,vla,vtit,NULL,scount,
				      save);
    }
  }
  write_end_table_html_file(fp); 

  if(tnhlinks <= 0) 
      return;

  i = 0;

  if(nhlinks > tnhlinks)
      tnlinks = tnhlinks;
  else
      tnlinks = nhlinks;

  sprintf(ntitle,"%d Most Visited Independent Hyperlinks to Non HTML page", 
	  tnlinks);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string3(fp,"Num","Hyperlink","Hits","Percentage");

  for(i = 1; i<=tnlinks ; i++)
  {
    WebIndependentLinkPtr il = (WebIndependentLinkPtr)randomized_select((GPTR *)varray2,
				       i-1,tnhlinks-1,1,
				       LOGR_is_access_il_lower,
				       LOGR_is_access_il_bigger);
    
    if(il)
    {
      const char *utit = LOGR_get_link_source_title(il);
      const char *vtit = LOGR_get_link_target_title(il);
      char *ula = (char *)LOGR_get_link_source_url(il);
      char *vla = (char *)LOGR_get_link_target_url(il);
      long nacc = LOGR_get_link_naccesses(il);
      float ave_nacc = ((float)nacc/(float)thits)*100.0;
      char snum[20],scount[20],save[20];      
      sprintf(snum,"%d", i);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      write_row_num_hyperlink_string2(fp,snum, ula,utit,vla,vtit,NULL,scount,
				      save);
    }
  }
  write_end_table_html_file(fp); 

  free(varray); varray = NULL; 
  free(varray2); varray2 = NULL; 
}

int 
report_shyperlinks_most_visited(GraphFrame *gf, char *name_file,
				int nhlinks,WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Most Visited Hyperlinks");
  write_start_body_html_file(fp,"#ffffff");
  write_body_shmvis_html_file(gf,fp,nhlinks,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_shmvis_html_file(GraphFrame *gf,FILE *fp,int nhlinks,
			    WebStatPtr msn)
{
  LNode_type *simple_links = LOGR_get_list_simple_links(msn);
  LNode_type *ptr = NULL;
  time_t now;
  WebGenLinkPtr *varray = NULL;
  WebGenLinkPtr *varray2 = NULL;
  int tlinks = simple_links ? count_llist(simple_links) : 0;
  int thlinks = 0;
  int tnhlinks = 0;
  int tnlinks = 0;
  int i = 0,j=0;
  char ntitle[100];
  long thits = LOGR_get_total_site_naccesses(msn);
  char *scolor = "beige";

  for(ptr = simple_links->back; ptr != simple_links; ptr=ptr->back)
  {
    WebGenLinkPtr il = (WebGenLinkPtr)ptr->info;
    if(LOGR_is_gen_html_target(il))
	thlinks++;
    else
	tnhlinks++;
  } 

  varray = thlinks ? (WebGenLinkPtr*)calloc(thlinks,
					   sizeof(WebGenLinkPtr)) : NULL;

  if(!varray && thlinks > 0) {
      fprintf(stderr,"Out of Memory write_body_shmvis_html_file\n");
      abort();
  }

  varray2 = tnhlinks ? (WebGenLinkPtr*)calloc(tnhlinks,
					   sizeof(WebGenLinkPtr)) : NULL;

  if(!varray2 && tnhlinks > 0) {
      fprintf(stderr,"Out of Memory write_body_shmvis_html_file\n");
      abort();
  }


  for(ptr = simple_links->back; ptr != simple_links; ptr=ptr->back)
  {
    WebGenLinkPtr il = (WebGenLinkPtr)ptr->info;

    if(LOGR_is_gen_html_target(il))
	varray[i++]=il;
    else
	varray2[j++]=il;
  } 

  now = time(NULL);
  write_header_html_file(fp,1,"List of Most Visited Hyperlinks");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Total Number of Requests : ");
  fprintf(fp, " %ld \n",thits);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Hyperlinks : ");
  fprintf(fp, " %d \n",tlinks);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RMVSHString);
  write_horizontal_rule_html_file(fp);

  if(thlinks <= 0) 
      return;

  i = 0;

  if(nhlinks > thlinks)
      tnlinks = thlinks;
  else
      tnlinks = nhlinks;

  sprintf(ntitle,"%d Most Visited Hyperlinks", tnlinks);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string3(fp,"Num","Hyperlink","Hits","Percentage");

  for(i = 1; i<=tnlinks ; i++)
  {
    WebGenLinkPtr il = (WebGenLinkPtr)randomized_select((GPTR *)varray,
				       i-1,thlinks-1,1,
				       LOGR_is_access_gen_link_lower,
				       LOGR_is_access_gen_link_bigger);
    
    if(il)
    {
      const char *utit = LOGR_get_gen_title_source(il);
      const char *vtit = LOGR_get_gen_title_target(il);
      const char *sanchor = LOGR_get_gen_link_label(il);
      char *ula = (char *)LOGR_get_gen_url_source(il);
      char *vla = (char *)LOGR_get_gen_url_target(il);
      long nacc = LOGR_get_gen_link_naccesses(il);
      float ave_nacc = ((float)nacc/(float)thits)*100.0;
      char snum[20],scount[20],save[20];      
      sprintf(snum,"%d", i);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      if(LOGR_is_gen_link_independent(il))
	  write_row_num_hyperlink_string2_color(fp,snum, ula,utit,vla,vtit,
						sanchor,scount,save,scolor);
      else
	  write_row_num_hyperlink_string2_color(fp,snum, ula,utit,vla,vtit,
						sanchor,scount,save,NULL);
    }
  }
  write_end_table_html_file(fp); 

  if(tnhlinks <= 0) 
      return;

  i = 0;

  if(nhlinks > tnhlinks)
      tnlinks = tnhlinks;
  else
      tnlinks = nhlinks;

  sprintf(ntitle,"%d Most Visited Hyperlinks to Non HTML page", 
	  tnlinks);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string3(fp,"Num","Hyperlink","Hits","Percentage");

  for(i = 1; i<=tnlinks ; i++)
  {
    WebGenLinkPtr il = (WebGenLinkPtr)randomized_select((GPTR *)varray2,
				       i-1,tnhlinks-1,1,
				       LOGR_is_access_gen_link_lower,
				       LOGR_is_access_gen_link_bigger);
    if(il)
    {
      const char *utit = LOGR_get_gen_title_source(il);
      const char *vtit = LOGR_get_gen_title_target(il);
      char *ula = (char *)LOGR_get_gen_url_source(il);
      char *vla = (char *)LOGR_get_gen_url_target(il);
      long nacc = LOGR_get_gen_link_naccesses(il);
      float ave_nacc = ((float)nacc/(float)thits)*100.0;
      char snum[20],scount[20],save[20];      
      sprintf(snum,"%d", i);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      if(LOGR_is_gen_link_independent(il))
	  write_row_num_hyperlink_string2_color(fp,snum, ula,utit,vla,vtit,
						NULL,scount,save,scolor);
      else
	  write_row_num_hyperlink_string2_color(fp,snum, ula,utit,vla,vtit,
						NULL,scount,save,NULL);
    }
  }
  write_end_table_html_file(fp); 

  free(varray); varray = NULL; 
  free(varray2); varray2 = NULL; 
}




